package com.myt.matp.gui;

import com.myt.matp.core.Event;
import com.myt.matp.utils.Edit;

import javax.microedition.lcdui.Graphics;

public class UIText extends UIEdit {

    protected UIText(int id, GUI gui) {
	super(id, gui);
    }

    public int getType() {
	return UIWidget.TEXT;
    }

    protected boolean isControlCharacter(char c) {
	return ((c == Edit.CHAR_NEWLINE) ||
		(c == Edit.CHAR_BRKLINE) ? true : false);
    }

    protected int getMaxHeight() {
	return getMinHeight();
    }

    public void setVerticalScrollBar(UIScrollBar sb) {
	super.setVerticalScrollBar(null);
    }
 
    protected int computeFrame() {
	int point;
	int x;
	int ret;
	int vw;

 	//#ifdef DEBUG_UITEXT
	//# System.out.println(getClass().getName() + ".computeFrame() {");
	//#endif

	point = m_SubEditor.getPoint();

	vw = getViewWidth();
	x = m_Gui.getCharWidth(m_SubEditor.getChar());
	while ((x < vw) && !m_SubEditor.atStart()) {
	    m_SubEditor.movePoint(-1);
	    x += m_Gui.getCharWidth(m_SubEditor.getChar());
	}

	if (x > vw) {
	    m_SubEditor.movePoint(1);
	}

	ret = m_SubEditor.getPoint();

	m_SubEditor.setPoint(point);

 	//#ifdef DEBUG_UITEXT
	//# System.out.println("}");
	//#endif

	return ret;
    }
  
    protected int computeRedisplay(int frame) {
	int point;
	int dl;
	int ret;
	int dw;

 	//#ifdef DEBUG_UITEXT
	//# System.out.println(getClass().getName() + ".computeRedisplay() {");
	//#endif

	point = m_SubEditor.getPoint();

	dw = getViewWidth();

	m_SubEditor.setPoint(frame);
	dl = m_Gui.getCharWidth(m_SubEditor.getChar());
	while ((dl < dw) && !m_SubEditor.atEnd()) {
	    m_SubEditor.movePoint(1);
	    dl += m_Gui.getCharWidth(m_SubEditor.getChar());
	}

	if (dl > dw) {
	    m_SubEditor.movePoint(-1);
	}

	ret = m_SubEditor.getPoint() - frame;

	m_SubEditor.setPoint(point);

 	//#ifdef DEBUG_UITEXT
	//# System.out.println("}");
	//#endif

	return ret;
    }

    protected void paintTextToImage(MGraphics g) {
	int point;
	int x;
	int dl;

 	//#ifdef DEBUG_UITEXT
	//# System.out.println(getClass().getName() + ".paintTextToImage() {");
	//#endif

	point = m_SubEditor.getPoint();
		
	x = getViewX();
	
	if (hasFlags(FLAG_CENTERED)) {
	    dl = m_Gui.getCharsWidth(m_SubEditor, 
				     getFrame(), 
				     getRedisplay());
	    x += ((getViewWidth() - dl) / 2);
	}

	g.setFont(getFontFace(), getFontStyle(), getFontSize());
	g.setColor(getColor(COLOR_FOREGROUND));
	
	m_SubEditor.setPoint(getFrame());
	while (m_SubEditor.getPoint() - getFrame() < getRedisplay()) {
	    
	    g.drawChar(m_SubEditor.getChar(),
		       x,
		       getViewY(), 
		       Graphics.TOP | Graphics.LEFT);
	    x += m_Gui.getCharWidth(m_SubEditor.getChar());
	    
	    m_SubEditor.movePoint(1);
	}
	
	m_SubEditor.setPoint(point);

 	//#ifdef DEBUG_UITEXT
	//# System.out.println("}");
	//#endif
    }
 
    public boolean dispatchEvent(final Event e) {
	boolean ret = true;
	if (!dispatchEventUIText(e)) {
	    ret = super.dispatchEvent(e);
	}
	return ret;
    }

    private boolean dispatchEventUIText(final Event e) {
	boolean ret = false;

	switch (e.getId()) {
	case Event.UI_KEY_RELEASED:
	    ret = true;
	    switch (e.getWParam()) {
	    case GUI.KEY_OK:
	    case GUI.KEY_DOWN:
		postEvent(Event.UI_TAB, GUI.KEY_DOWN);
		break;
	    case GUI.KEY_UP:
		postEvent(Event.UI_TAB, GUI.KEY_UP);
		break;
	    default:
		ret = false;
	    }
	    break;
	}

	return ret;
    }

}